<?php
/**
 * Created by PhpStorm.
 * User: geyouwen
 * Date: 16/8/30
 * Time: 下午11:25
 */

namespace Home\Controller;


use Think\Controller;

class MemberController extends Controller
{
    private $uid;
    public function __construct()
    {
        parent::__construct();
        if(!session('current_member'))
        {
            $this->redirect(U('passport/login'));
        }
        $login_account = session('current_member');
        $this->assign('login_account', $login_account);
        $this->uid = $login_account['uid'];
        $site_conf = $this->_siteSettings();
        $this->assign('site', $site_conf);
        $this->assign('module_name', strtolower(MODULE_NAME) );
        $this->assign('controller_name', strtolower(CONTROLLER_NAME) );
        $this->assign('action_name', strtolower(ACTION_NAME));
    }

    public function center()
    {
        if(IS_AJAX)
        {
            if(D('member')->create())
            {
                D('member')->where(array('id' => $this->uid))->save();
                $back = array('status' => 1, 'msg' => '保存成功');
            } else{
               $msg =  D('member')->getError();
               $back = array('status' => 0, 'msg' => $msg);
            }
            $this->ajaxReturn($back);
        }

        $info = D('member')->find($this->uid);
        $this->assign('info', $info);
        $this->display('center');
    }

    //安全中心
    public function password()
    {
        if(IS_AJAX)
        {
            $password = I('post.password');
            $newpass = I('post.newpass');
            if(strlen($newpass) < 6)
            {
                $back = array('status' => 0, 'msg' => '新密码至少6位');
                $this->ajaxReturn($back);
            }
            $info = D('member')->find($this->uid);
            if($info['password'] != md5($password))
            {
                $back = array('status' => 0, 'msg' => '旧密码错误');
                $this->ajaxReturn($back);
            }
            $confirm = I('post.confirmpass');
            if($confirm != $newpass)
            {
                $back = array('status' => 0, 'msg' => '两次密码输入不一致');
                $this->ajaxReturn($back);
            }
            $data = array('password' => md5($newpass));
            D('member')->where(array('id' => $this->uid))->save($data);
            $back = array('status' => 1, 'msg' => '密码修改成功');
            $this->ajaxReturn($back);
        }

        $this->display();
    }

    //家庭成员
    public function family()
    {
        $list = M('member_relation')->where(array('uid' => $this->uid))->select();
        $this->assign('list', $list);
        $this->display();
    }

    //积分 等级
    public function score()
    {
        $info = D('member')->find($this->uid);
        $level = M('level')->where(array('id' =>$info['level_id']))->getField('name');
        if(!$level) $level = '注册用户';
        $this->assign('info', $info);
        $this->assign('level', $level);
        $this->display();
    }

    private function _siteSettings()
    {
        if(S('settings'))
        {
            $data = M('settings')->select();
            foreach($data as $key => $val)
            {
                $k = str_replace('setting_', '', $val['name']);
                $tmp[$k] = unserialize($val['data']);
            }
            S('settings', $tmp, 86400);
        }
        return S('settings');
    }

}